package com.example.ssm.service.impl;

import com.example.ssm.QueryVO;
import com.example.ssm.mapper.CustomerMapper;
import com.example.ssm.pojo.Customer;
import com.example.ssm.pojo.CustomerExample;
import com.example.ssm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("customerService")
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Customer> list() {
        return customerMapper.selectByExample(null);
    }

    //无分页用户列表显示
    @Override
    public List<Customer> list(QueryVO queryVO) {

        PageHelper.startPage(queryVO.getPage(),queryVO.getRows());

        //sql查询语句，或者在xml中写
        //mybatis逆向工程生成spl语句的类使用方式
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();

        if(queryVO.getCustName()!=null&&queryVO.getCustName().length()>0){
            criteria.andCustNameLike("%"+queryVO.getCustName()+"%");
        }

        if(queryVO.getCustSource()!=null&&queryVO.getCustSource().length()>0){
            criteria.andCustSourceEqualTo(queryVO.getCustSource());
        }

        if(queryVO.getCustIndustry()!=null&&queryVO.getCustIndustry().length()>0){
            criteria.andCustIndustryEqualTo(queryVO.getCustIndustry());
        }

        if(queryVO.getCustLevel()!=null&&queryVO.getCustLevel().length()>0){
            criteria.andCustLevelEqualTo(queryVO.getCustLevel()) ;
        }

        List<Customer> list = customerMapper.selectByExample(customerExample);
        return list;
    }

    //有分页用户列表显示
    @Override
    public PageInfo<Customer> list_bak(QueryVO queryVO) {

        PageHelper.startPage(queryVO.getPage(),queryVO.getRows());

        //sql查询语句，或者在xml中写
        //mybatis逆向工程生成spl语句的类使用方式
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();

        if(queryVO.getCustName()!=null&&queryVO.getCustName().length()>0){
            criteria.andCustNameLike("%"+queryVO.getCustName()+"%");
        }

        if(queryVO.getCustSource()!=null&&queryVO.getCustSource().length()>0){
            criteria.andCustSourceEqualTo(queryVO.getCustSource());
        }

        if(queryVO.getCustIndustry()!=null&&queryVO.getCustIndustry().length()>0){
            criteria.andCustIndustryEqualTo(queryVO.getCustIndustry());
        }

        if(queryVO.getCustLevel()!=null&&queryVO.getCustLevel().length()>0){
            criteria.andCustLevelEqualTo(queryVO.getCustLevel()) ;
        }

        List<Customer> list = customerMapper.selectByExample(customerExample);
        PageInfo<Customer> pageInfo = new PageInfo<Customer>(list);
        return pageInfo;
    }

    //增加
    @Override
    public void addCustomer(Customer customer)
    {
        customerMapper.insert(customer);
    }
    //查询
    @Override
    public Customer queryCustomerById(Long id)
    {
        return customerMapper.selectByPrimaryKey(id);
    }
    //修改
    @Override
    public void updateCustomerById(Customer customer)
    {
        customerMapper.updateByPrimaryKeySelective(customer);
    }
    //删除
    @Override
    public void deleteCustomerById(Long id) {
        customerMapper.deleteByPrimaryKey(id);
    }
}
